/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package descriptorarchivos;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ascalero
 */
public class Tabla implements Serializable {
    private int noCampos;
    private String nombre;    
    private String nombreatrib[];
    private int nTuplas;
    private ArrayList <Tupla> alpha= new ArrayList<>();
    public Tabla(){
        nTuplas= 0;
    }
     public Tabla(int a){
        nTuplas= 0;
        noCampos=a;
    }
    public Tabla(String nom, int ncampos){
        noCampos=ncampos;
        nTuplas= 0;
        nombre=nom;
        nombreatrib= new String[noCampos];
        setnomAttrib();
    }
    
    
    public ArrayList<Tupla> getValores(){
    return alpha;
    }
    
    public String[] getNAttrib(){
    return nombreatrib;
    }
    
    public int getSize(){return noCampos;}
        
    public void addTupla(){
    Tupla a= new Tupla(noCampos);
    a.setCampos();
    alpha.add(a);
    nTuplas++;
    }
    
    public void addTupla(String x[]){
    Tupla a= new Tupla(noCampos);
    a.setCampos(x);
    alpha.add(a);
    nTuplas++;
    }
    
    public void guardar(){
        try{
            String nam= nombre+".tab";
            FileOutputStream fs= new FileOutputStream(nam);
            ObjectOutputStream os= new ObjectOutputStream(fs);
            os.writeObject(this);
            os.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    
        
    private void setnomAttrib(){
    System.out.println("Escribe el nombre de los atributos");
        for(int i=0;i<noCampos;i++){
        
           Scanner a= new Scanner(System.in);
           System.out.print(i+".-");
           nombreatrib[i]=a.nextLine();
       }   
    }
    public void setnomAttrib(String[] x){
       nombreatrib=x;
    }
    public void displayNameFields(){
        for(int i=0;i<noCampos;i++)
        System.out.print(i+".-"+nombreatrib[i]+"   ");
        
    }
    public int displayContent(){
        displayNameFields();
        if (alpha.isEmpty()){ return 1;}
        System.out.println();
        for(Tupla x:alpha){
            x.displaycampos();
            System.out.println();
        }
        return 0;
    }
    
    
    
    
}
